package com.xbongbong.saas.domain.entity;

import com.xbongbong.paas.toolbox.util.Arith;
import lombok.Getter;
import lombok.Setter;

import java.util.Objects;

@Getter
@Setter
public class ProductStockUpdateEntity {
    //唯一id,用于更新产品表
    private Long productId;
    //唯一key，用于更新分仓批次
    private String key;
    //总库存
    private Double stock;
    //总成本
    private Double cost;

    /**
     * 初始化，置入库存，成本
     * @param warehouseKey
     * @param amendNum 库存
     * @param amendCost 单位成本
     */
    public ProductStockUpdateEntity(String warehouseKey, Double amendNum, Double amendCost) {
        this.key = warehouseKey;
        this.stock = amendNum;
        this.cost = Arith.mul(amendNum,amendCost);
    }

    public ProductStockUpdateEntity(Long productId, Double amendNum, Double amendCost) {
        this.productId = productId;
        this.stock = amendNum;
        this.cost = Arith.mul(amendNum,amendCost);
    }
    public ProductStockUpdateEntity(Long productId, Double cost) {
        this.productId = productId;
        this.stock = 0D;
        this.cost = cost;
    }

    /**
     * 更新库存、成本
     * @param amendNum 库存
     * @param amendCost 单位成本
     */
    public void updateStock(Double amendNum, Double amendCost) {
        this.stock = Arith.add(this.stock,amendNum);
        this.cost = Arith.add(this.cost,Arith.mul(amendNum,amendCost));
    }

    public Double getUnitCost() {
        if (!Objects.equals(this.stock,0D)){
            return Arith.div(this.cost,this.stock);
        }
        return 0D;
    }
}
